<template>
  <a-card :bordered="false">
    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row.Id||index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">查看</a>
          <a-divider v-if="hasPerm('SO_SOMain.Edit') && hasPerm('SO_SOMain.Delete')" type="vertical"/>
          <a v-if="hasPerm('SO_SOMain.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import moment from 'moment'

const columns = [
  { title: '客户名称', dataIndex: 'CustomerName' },
  { title: '组名称', dataIndex: 'GroupName' },
  { title: '销售经理', dataIndex: 'SalesmanName' },
  { title: '角色', dataIndex: 'RoleNameList' }
]

export default {
  props: {
    Uid: { require: true, type: String }
  },

  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      time: null,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'Id' },
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http.post('/CustomerManagement/Cust_CustomerAllocation/GetDataListByCustomerIdForAllocationInfo', {
        PageIndex: this.pagination.current,
        PageRows: this.pagination.pageSize,
        SortField: this.sorter.field || 'Id',
        SortType: this.sorter.order,
        Search: [{ condition: 'CustomerId', keyword: this.Uid }], ...this.filters
      }).then(resJson => {
        this.loading = false
        resJson.Data.forEach(item => {
          item.RoleNameList = item.RoleNameList.join(',')
        })
        this.data = resJson.Data
        if (this.data) {
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        }
      })
    },

    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建销售订单主表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '订单详情')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/OrderManagement/SO_SOMain/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
